package com.vm.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;
import java.util.Properties;

public class ConfigManager {
    private static Properties pro = new Properties();
    private static File env;
    private static final String AIS_IP_KEY="ais_ip";
    private static final String AIS_NAME_KEY="ais_name";
    private static final String AIS_PWD_KEY="ais_pwd";
    private static final String AIS_PORT_KEY="ais_port"; 
    private static final String JNLP_HREF="jnlp_href"; 
    
    private static final String S57_8081_URL="sul8081"; 
    private static final String S57_8082_URL="sul8082"; 
    private static final String WELECOM_LAT="wellat"; 
    private static final String WELECOM_LOG="wellog"; 

    private static Properties getProperties() {
        URL url =Thread.currentThread().getContextClassLoader().getResource("env.property");
        
       // ConfigManager.class.getClassLoader().getResource("env.property");
        env = new File(url.getFile());
        
        try {
            pro.load(new FileInputStream(env));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return pro;
    }

    public static String getEnv(String key) {
        loadIfNeeded();
        return pro.getProperty(key, null);
    }

    private static void loadIfNeeded() {
        if (pro != null && pro.size() == 0) {
            pro = getProperties();
        }
    }

    public static String getEnvFilePath() {
        return (env == null) ? null : env.getAbsolutePath();
    }
    
    
    public static String  getAisIP(){
    	String result = getKeyUtil(AIS_IP_KEY);
    	return result;
    }
     
    public static String  getAisName(){
    	String result = getKeyUtil(AIS_NAME_KEY);
    	return result;
    }
    public static String getJnlpHref(){
    	String result = getKeyUtil(JNLP_HREF);
    	return result;
    }
    public static String  getAisPwd(){
    	String result = getKeyUtil(AIS_PWD_KEY);
    	return result;
    }
   
    public static String  getAisPort(){
    	String result = getKeyUtil(AIS_PORT_KEY);
    	return result;
    }
    
    public static String  getS578081(){
    	String result = getKeyUtil(S57_8081_URL);
    	return result;
    }
    
    public static String  getS578082(){
    	String result = getKeyUtil(S57_8082_URL);
    	return result;
    }
    
    public static String  getWellat(){
    	String result = getKeyUtil(WELECOM_LAT);
    	return result;
    }

    public static String  getWellog(){
    	String result = getKeyUtil(WELECOM_LOG);
    	return result;
    }
    
    public static String  getKeyUtil(String key)
    {
    	String result = "";
    	try{
    		result =getProperties().getProperty(key);
    	}catch (Exception e) {
    		e.printStackTrace();
			// TODO: handle exception
		}
    	return result;
    }
}
